package com.fira.web.controller.cms;

import java.util.List;

import com.fira.common.utils.DateUtils;
import com.fira.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fira.common.annotation.Log;
import com.fira.common.enums.BusinessType;
import com.fira.cms.domain.CmsArticle;
import com.fira.cms.service.ICmsArticleService;
import com.fira.common.core.controller.BaseController;
import com.fira.common.core.domain.AjaxResult;
import com.fira.common.utils.poi.ExcelUtil;
import com.fira.common.core.page.TableDataInfo;

/**
 * 文章信息发布Controller
 * 
 * @author tony
 * @date 2021-10-09
 */
@Controller
@RequestMapping("/cms/article")
public class CmsArticleController extends BaseController
{
    private String prefix = "cms/article";

    @Autowired
    private ICmsArticleService cmsArticleService;

    @RequiresPermissions("cms:article:view")
    @GetMapping()
    public String article()
    {
        return prefix + "/article";
    }

    /**
     * 查询文章信息发布列表
     */
    @RequiresPermissions("cms:article:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CmsArticle cmsArticle)
    {
        startPage();
        List<CmsArticle> list = cmsArticleService.selectCmsArticleList(cmsArticle);
        return getDataTable(list);
    }

    /**
     * 导出文章信息发布列表
     */
    @RequiresPermissions("cms:article:export")
    @Log(title = "文章信息发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsArticle cmsArticle)
    {
        List<CmsArticle> list = cmsArticleService.selectCmsArticleList(cmsArticle);
        ExcelUtil<CmsArticle> util = new ExcelUtil<CmsArticle>(CmsArticle.class);
        return util.exportExcel(list, "article");
    }

    /**
     * 新增文章信息发布
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存文章信息发布
     */
    @RequiresPermissions("cms:article:add")
    @Log(title = "文章信息发布", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsArticle cmsArticle)
    {
        return toAjax(cmsArticleService.insertCmsArticle(cmsArticle));
    }

    /**
     * 修改文章信息发布
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CmsArticle cmsArticle = cmsArticleService.selectCmsArticleById(id);
        mmap.put("cmsArticle", cmsArticle);
        return prefix + "/edit";
    }

    /**
     * 修改保存文章信息发布
     */
    @RequiresPermissions("cms:article:edit")
    @Log(title = "文章信息发布", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsArticle cmsArticle)
    {
        return toAjax(cmsArticleService.updateCmsArticle(cmsArticle));
    }

    /**
     * 删除文章信息发布
     */
    @RequiresPermissions("cms:article:remove")
    @Log(title = "文章信息发布", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cmsArticleService.deleteCmsArticleByIds(ids));
    }

    @GetMapping("/toAudit/{id}")
    public String toAudit(@PathVariable String id,ModelMap modelMap){
        modelMap.put("cmsArticle",cmsArticleService.selectCmsArticleById(id));
        return prefix + "/audit";
    }

    @RequiresPermissions("cms:article:audit")
    @Log(title = "审核文章信息发布", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    @ResponseBody
    public AjaxResult audit(CmsArticle cmsArticle){
        cmsArticle.setAuditBy(ShiroUtils.getSysUser().getUserName());
        cmsArticle.setAuditTime(DateUtils.getNowDate());
        return toAjax(cmsArticleService.updateCmsArticle(cmsArticle));
    }
}
